const enterpriseVue = new Vue({
  el: "#app",
  data() {
    return {
      three: false,
      zz: [],
      multipleSelection: [],
      employeeqx: false,
      gridData: [],
      ADD: [],
      modify: [],
      checked: [],
      pickerOptions: pickerOptions,
      rules: {},
      form: {},
      menuList: [],
      menuList1: [],
      UserNumber: "",
      allCheckedKeys: [],
      loading: false,
    };
  },
  mounted() {
    let yg = sessionStorage.getItem("yg");
    console.log(yg);
    if (yg == !null) {
      this.$alert("请先选择要分配权限的员工", "提示", {
        confirmButtonText: "我已知晓",
        type: "warning",
      });
    }

    const User_Number = sessionStorage.getItem("User_Number");
    this.User_Number = User_Number;
    this.getData();
    this.setCheckMenu(User_Number);
  },
  methods: {
    renderTree(tree, a) {
      const _this = this;
      return tree.map((i) => ({
        ...i,
        disabled: a,
        children: this.renderTree(i.children, a),
      }));
    },
    submitForm2() {
      //500
      console.log(this.ADD);
      // return;
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        contentType: "application/json", // 设置请求体的媒体类型为application/json
        type: "POST",
        // url: "http://192.168.10.86:8087/Enterprise/addEmployee",
        url: "https://service.kdddy.cn/donghuiForRequest/Enterprise/addEmployee",
        data: JSON.stringify(this.ADD),
        success: function (Result) {
          console.log(Result);
          // window.history.go(-1);
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    qd() {
      console.log(this.multipleSelection);
      this.employeeqx = false;
      const newTree = this.renderTree(this.menuList1, false);
      this.menuList1 = newTree;
    },
    employee() {
      this.employeeqx = true;
      let Enterprise_Info_SerialNumber = sessionStorage.getItem(
        "Enterprise_Info_SerialNumber"
      );
      console.log(Enterprise_Info_SerialNumber);
      let System_SerialNumber = sessionStorage.getItem("System_SerialNumber");
      const _this = this;
      $.ajax({
        // headers: requestHeaders(),
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        // url: "http://192.168.10.86:8087/Enterprise/getEmployeeNoRights",
        url: "https://service.kdddy.cn/donghuiForRequest/Enterprise/getEmployeeNoRights",
        type: "get",
        data: {
          eid: Enterprise_Info_SerialNumber, //表名
          sid: System_SerialNumber,
        },
        success: function (Result) {
          console.log(Result);

          _this.gridData = Result.Data;
          // _this.total = Result.TotalRows;
        },
        // complete: function (XMLHttpRequest) {
        // requestPretreatment(XMLHttpRequest);
        // },
        dataType: "json",
      });
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
    //提交
    submitForm() {
      // 600

      // this.checked.forEach((Item) => {
      // console.log(Item);

      // 假设你有一个 ref 指向 el-tree 组件
      // const tree = this.$refs.tree;
      // 使用数据对象获取节点 const node = tree.getNode(dataObject);
      // 或者使用节点的 key 获取节点（如果设置了 node-key 属性）
      // console.log(tree.getNode(Item));
      //   let nodeByKey = tree.getNode(Item);
      //   const checkedKeys = tree.getCheckedKeys();
      //   Object.keys(nodeByKey).forEach((key) => {
      //     if (!nodeByKey.checked) {
      //       nodeByKey[key + "_Original"] = nodeByKey[key];
      //       nodeByKey.UR_New = "0";
      //     }
      //   });
      // });
      // console.log(this.checked);

      console.log(this.modify);
      // this.modify.forEach((element) => {
      //   Object.keys(element).forEach((key) => {
      //     element[key + "_Original"] = element[key];
      //     // nodeByKey.UR_New = "0";
      //   });
      //   element.UR_New = "0";
      // });
      // console.log(this.modify);

      // return;
      let _this = this;
      _this.loading = true;
      console.log(this.ADD);
      let lJSONObject_system_rights_user_model = new Object();
      lJSONObject_system_rights_user_model.system_rights_user_model = this.ADD; //换表
      let lJSONObject_system_rights_user_model2 = new Object();
      lJSONObject_system_rights_user_model2.system_rights_user_model =
        this.modify;
      let lJSONObject_DataSet = new Object(); //Modify
      lJSONObject_DataSet.Add = lJSONObject_system_rights_user_model; //换表
      lJSONObject_DataSet.Modify = lJSONObject_system_rights_user_model2;
      console.log(lJSONObject_DataSet);

      // return;
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (res) {
          console.log(res);
          if (res.Status === "success") {
            _this.$message.success("提交成功");
            window.history.go(-1);
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试");
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false;
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href =
              absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
      });
    },
    getData() {
      let User_Number = sessionStorage.getItem("User_Number");
      let Enterprise_Info_SerialNumber = sessionStorage.getItem("qy");
      console.log(User_Number);
      console.log(Enterprise_Info_SerialNumber);
      let _this = this;
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        type: "GET",
        // url: `${DomainName}/SystemMenu?token=${$.cookie('token')}&system=${JSON.parse($.cookie("system")).System_SerialNumber}`,
        // url: "http://192.168.10.70:8087/User/setRightList",
        url: "https://service.kdddy.cn/donghuiForRequest/User/setRightList",
        data: {
          User_Number: User_Number, //表名
          Enterprise_Info_SerialNumber: sessionStorage.getItem("qy"), //值
          System_SerialNumber: sessionStorage.getItem("System_SerialNumber"),
        },
        // complete: function (XMLHttpRequest) {
        //   _this.loading = false;
        //   if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
        //     window.parent.document.location.href =
        //       absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
        //   }
        // },
        success: function (Result) {
          console.log("aaa", Result);
          // if (condition) {
            
          // }
          // Result.data1.forEach(item=>{
          //   if (item.children) {
          //     item.children =  item.children.filter(i=>i.Model_Number != 'M01000000000170')
          //   }
          // })
          console.log(Result);
          _this.menuList1 = _this.renderTree(Result.data1, true);
          _this.menuList = Result.data1; //将获取的数据存到表格数据中
          _this.checked = Result.data2;
        },
        dataType: "json",
      });
    },
    handleSelectionChange2(data) {
      this.three = true;
      console.log(data);
      // if (data.UR_New == "1") {
      //   data.UR_New = "0";
      // } else {
      //   data.UR_New = "1";
      // }
      console.log(typeof data.Add);
      // return;
      if (typeof data.Add == "undefined") {
        data.Add = 0;
        // data.UR_New = "1";
        console.log(this.multipleSelection);
        console.log(this.multipleSelection[0].Employee_SerialNumber);
        // return;
        data.Enterprise_Info_SerialNumber =
          this.multipleSelection[0].Employee_SerialNumber;
        data.Employee_Name = this.multipleSelection[0].Employee_Name;
        data.Mobile = this.multipleSelection[0].Mobile;
        data.Enterprise_Info_SerialNumber =
          this.multipleSelection[0].Enterprise_Info_SerialNumber;
        data.ID_Number = this.multipleSelection[0].ID_Number;

        this.ADD.push(data);
        let a = this.ADD?.filter((i) => i.Add);

        console.log(a);
      } else if (typeof data.Add !== "undefined") {
        delete data.Add;
        data.UR_New == " ";
        const index = this.ADD.indexOf(data);
        if (index !== -1) {
          this.ADD.splice(index, 1); // 从 modify 中移除 data
        }
      }

      // 查看状态

      console.log("ADD:", this.ADD);
      // }
    },
    handleCheckChange(data) {
      console.log(data);
      // console.log(typeof data.Add);
      // return;
      if (data.UR_New == "" && typeof data.Add == "undefined") {
        data.Add = 0;
        data.UR_New = "1";
        data.Enterprise_Info_SerialNumber = sessionStorage.getItem("qy");
        data.User_Number = sessionStorage.getItem("zh");

        this.ADD.push(data);
        let a = this.ADD?.filter((i) => i.Add);
        console.log(a);
      } else if (data.UR_New !== "" && typeof data.Add !== "undefined") {
        delete data.Add;
        data.UR_New = "";
        const index = this.ADD.indexOf(data);
        if (index !== -1) {
          this.ADD.splice(index, 1); // 从 modify 中移除 data
        }
      } else if (data.UR_New !== "" && typeof data.modify == "undefined") {
        if (JSON.stringify(data).indexOf("_Original") == -1) {
          //创建 Original

          Object.keys(data).forEach((key) => {
            data[key + "_Original"] = data[key];
            // nodeByKey.UR_New = "0";
          });
        }
        if (data.UR_New == "1") {
          data.UR_New = "0";
        } else {
          data.UR_New = "1";
        }
        data.modify = 0;
        data.Enterprise_Info_SerialNumber = sessionStorage.getItem("qy");
        data.User_Number = sessionStorage.getItem("zh");
        let a = this.modify?.filter((i) => i.modify);
        console.log(a);
        // Object.keys(data).forEach((key) => {
        //   data[key + "_Original"] = data[key];
        //   data.UR_New = "1";
        // });
        this.modify.push(data);
        console.log(data);
      } else if (data.UR_New !== "" && typeof data.modify !== "undefined") {
        data.UR_New = data.UR_New_Original;
        delete data.modify;
        const index = this.modify.indexOf(data);
        if (index !== -1) {
          this.modify.splice(index, 1); // 从 modify 中移除 data
        }
      }

      // 查看状态

      console.log("ADD:", this.ADD);
      console.log("modify:", this.modify);

      // console.log(this.checked);

      // let halfKeys = this.$refs.tree.getHalfCheckedKeys();
      // console.log(halfKeys);

      // let keys = this.$refs.tree.getCheckedKeys();
      // console.log(keys);

      // this.allCheckedKeys = [...keys, ...halfKeys];
      // console.log(this.allCheckedKeys);
    },
    setCheckMenu(UserNumber) {
      let _this = this;
      $.ajax({
        type: "POST",
        url: DomainName + "/RoleAction?token=" + $.cookie("token"),
        data: {
          UserNumber,
        },
        // complete: function (XMLHttpRequest) {
        //   _this.loading = false;
        //   if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
        //     window.parent.document.location.href =
        //       absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
        //   }
        // },
        success: function (Result) {
          console.log(Result);
          _this.handleCheckChange();
          _this.$refs.tree.setCheckedKeys(Result, true);
        },
        dataType: "json",
      });
    },
  },
});
